package tr.mesir.service;

import java.util.List;

import org.apache.lucene.queryParser.ParseException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import tr.mesir.dao.AddressBookDAO;
import tr.mesir.dao.ContactDAO;
import tr.mesir.domain.AddressBook;
import tr.mesir.domain.wrappers.ContactVersionWrapper;

/**
 * This is the service class that is injected into the controller. 
 * @Service annotation indicates that this class is used as a business facade by controller beans.
 * 
 * @author mertcaliskan
 *
 */
@Service("addressBookService")
public class AddressBookServiceImpl implements AddressBookService {
	
	@Autowired
	private AddressBookDAO addressBookDAO;

	@Autowired
	private ContactDAO contactDAO;
	
	/* By declaring the tx:annotation-driven in applicationContext, methods/classes can be annotated 
	 * as transactional and treated in that context.
	 */
	@Transactional	
	public void save(AddressBook addressBook) {
		addressBookDAO.save(addressBook);
	}

	public List<AddressBook> findAll() {
		return addressBookDAO.loadAll();
	}

	public List<String> findAllTitles() {
		return addressBookDAO.findAllTitles();
	}
	
	public AddressBook findById(Long id) {
		return addressBookDAO.loadById(id);
	}

	@Transactional
	public void delete(AddressBook addressBook) {
		addressBookDAO.delete(addressBook);
	}

	/* Updates also have the transactional context, to get the envers in action.
	 */
	@Transactional
	public void update(AddressBook addressBook) {
		addressBookDAO.update(addressBook);
	}

	public List<AddressBook> findByWord(String searchWord) throws ParseException {
		return addressBookDAO.findByWord(searchWord);
	}

	public List<AddressBook> getAddressBookVersions(Long id) {
		return addressBookDAO.getAddressBookVersions(id);
	}

	public List<ContactVersionWrapper> getContactVersions(Long id) {
		return contactDAO.getContactVersions(id);
	}
}